package com.game.avatar
{

	import com.game.events.GameEventDispatcher;
	import com.game.render.RenderData;
	import com.net.interfaces.IDestroy;

	import flash.utils.Dictionary;

	public class Avatar implements IDestroy
	{
		public var render : RenderData;

		/**
		 * 当前播放的动画
		 */
		public var curAnimation : Animation;

		/**
		 * 当前动作名称
		 */
		public var curAction : int;

		public var actionsDirAnimation : Dictionary;

		public var scaleX : int;

		/**
		 * 方向只有左右区别
		 * 上下用当前的curkey区分
		 */
		public var dir : int;

		private var avatarId : String;

		public function Avatar(avatarId : String)
		{
			this.avatarId = avatarId;
			render = new RenderData();
		}

		/**
		 * 更换动作,方向
		 * @param action 动作名称
		 * @param dir    方向
		 * @param index  帧数
		 * @param loops  循环次数
		 *
		 */
		public function playAvatar(action : int, dir : int, index : int = 0, loops : int = 0) : Frame
		{
			curAction = action;
			curAnimation = actionsDirAnimation[action] as Animation;

			if (curAnimation == null)
			{
				GameEventDispatcher.sendError("缺少avatar：", avatarId, action);
				return null;
			}

			this.dir = dir;
			curAnimation.loops = loops;
			return curAnimation.gotoFrame(index);
		}

		/**
		 * 换方向
		 * @param dir [0-7]
		 * @return
		 */
		public function palyDir(dir : int) : Frame
		{
			return playAvatar(curAction, dir, curAnimation ? curAnimation.curFrameIndex : 0);
		}

		public function hasAction(action : int) : Boolean
		{
			if (actionsDirAnimation == null)
			{
				return false;
			}
			return actionsDirAnimation[action] != null;
		}

		/**
		 * 尝试是否可以播放下一帧
		 * @param elapsedTime
		 * @param duration
		 * @return
		 *
		 */
		public function tryNext(elapsedTime : int, duration : int = 0) : Frame
		{
			var frame : Frame = curAnimation ? curAnimation.tryNext(elapsedTime, duration) : null;
			return frame;
		}

		public function updateRender(sceneX : int, sceneY : int, offsetX : int = 0, offsetY : int = 0) : void
		{
			if (curAnimation == null)
			{
				render.bitmapData = null;
				return;
			}
			var frame : Frame = curAnimation.curFrame;

			if (frame == null || frame.data == null)
			{
				return;
			}
			render.bitmapData = frame.data;
			render.scaleX = scaleX;

			if (scaleX == 1)
			{
				render.x = sceneX + frame.offsetX + offsetX;
			}
			else
			{
				render.x = sceneX - frame.offsetX + offsetX;
			}

			render.y = sceneY + frame.offsetY;
		}

		/**
		 * 重置动作帧数
		 *
		 */
		public function resetAvatar() : void
		{
			if (curAnimation)
			{
				curAnimation.resetAnimation();
			}
		}

		public function isEnd() : Boolean
		{
			if (!curAnimation)
				return false;
			return curAnimation.isEnd;
		}

		public function get isDestroy() : Boolean
		{
			return false;
		}

		public function destroy() : void
		{
			if (render)
			{
				render.destory()
				render = null;
			}
			curAnimation = null;
			actionsDirAnimation = null;
		}


	}
}